<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2011 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  2011 Andreas Koob 
 * @author     Andreas Koob 
 * @package    tasks 
 * @license    LGPL 
 * @filesource
 */


/**
 * Table tl_tasks_priorities
 */
$GLOBALS['TL_DCA']['tl_tm_priorities'] = array
(

	// Config
	'config' => array
	(
		'dataContainer'               => 'Table',
		'enableVersioning'            => true,
		'onsubmit_callback' => array
		(
			array('tl_tm_priorities', 'processOnsubmit')
		),
		'onload_callback' => array
		(
			array('tl_tm_priorities', 'processOnload')
		)
	),

	// List
	'list' => array
	(
		'sorting' => array
		(
			'mode'                    => 1,
			'fields'                  => array('priority_order'),
			'flag'                    => 12,
			'disableGrouping'		  => true
		),
		'label' => array
		(
			'fields'                  => array('name'),
			'format'                  => '%s'
		),
		'global_operations' => array
		(
			'all' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['MSC']['all'],
				'href'                => 'act=select',
				'class'               => 'header_edit_all',
				'attributes'          => 'onclick="Backend.getScrollOffset();" accesskey="e"'
			)
		),
		'operations' => array
		(
			'edit' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tasks_priorities']['edit'],
				'href'                => 'act=edit',
				'icon'                => 'edit.gif'
			),
			'copy' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tasks_priorities']['copy'],
				'href'                => 'act=copy',
				'icon'                => 'copy.gif'
			),
			'delete' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_priorities']['delete'],
				'href'                => 'act=delete',
				'icon'                => 'delete.gif',
				'attributes'          => 'onclick="if (!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\')) return false; Backend.getScrollOffset();"'
			),
			'show' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_priorities']['show'],
				'href'                => 'act=show',
				'icon'                => 'show.gif'
			),
			'up' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_priorities']['up'],
				'href'                => 'act=up',
				'icon'                => 'up.gif'
			),
			'down' => array
			(
				'label'               => &$GLOBALS['TL_LANG']['tl_tm_priorities']['down'],
				'href'                => 'act=down',
				'icon'                => 'down.gif'
			)
		)
	),

	// Palettes
	'palettes' => array
	(
		'__selector__'                => array(''),
		'default'                     => 'name'
	),

	// Subpalettes
	'subpalettes' => array
	(
		''                            => ''
	),

	// Fields
	'fields' => array
	(
		'name' => array
		(
			'label'                   => &$GLOBALS['TL_LANG']['tl_tm_priorities']['name'],
			'exclude'                 => true,
			'inputType'               => 'text',
			'eval'                    => array('mandatory'=>true, 'maxlength'=>255)
		)
	)
);

class tl_tm_priorities extends Backend
{
	public function processOnsubmit(DataContainer $dc)
	{
		if (!$dc->activeRecord)
		{
			return;
		}
		if($dc->activeRecord->priority_order=='0')
		{
			$arrPriority = $this->Database->prepare("SELECT max(priority_order) AS max_priority_order FROM tl_tm_priorities")->execute();
			if ($arrPriority->numRows==0){
				$priority_order=1;
			}
			else{
				$priority_order= $arrPriority->max_commentno + 1;
			}
			$this->Database->prepare("UPDATE tl_tm_priorities SET priority_order=? WHERE id=?")->execute($priority_order, $dc->id);
			return;
		}
	}//public function processOnsubmit
	
	public function processOnload()
	{
		$id=$this->Input->get('id');
		$act=$this->Input->get('act');
		switch($act)
		{
			case "down":
				$currentPriority=$this->Database->prepare("SELECT * FROM tl_tm_priorities WHERE id=?")->execute($id);
				$previousPriority=$this->Database->prepare("SELECT * FROM tl_tm_priorities WHERE priority_order<? ORDER BY priority_order DESC LIMIT 0,1")->execute($currentPriority->priority_order);
				if ($previousPriority->numRows > 0) //Is there a previous round?
				{
					//Update current project
					$this->Database->prepare("UPDATE tl_tm_priorities SET priority_order=? WHERE id=?")->execute($previousPriority->priority_order,$currentPriority->id);
					//Update previous project
					$this->Database->prepare("UPDATE tl_tm_priorities SET priority_order=? WHERE id=?")->execute($currentPriority->priority_order,$previousPriority->id);
				}
				//Go back to inital page. Otherwise contao will show a blank list
				$this->redirect('contao/main.php?do=tl_tm_priorities&id=' . $currentPriority->pid);
				break;
			case "up":
				$currentPriority=$this->Database->prepare("SELECT * FROM tl_tm_priorities WHERE id=?")->execute($id);
				$nextPriority=$this->Database->prepare("SELECT * FROM tl_tm_priorities WHERE priority_order>? ORDER BY priority_order ASC LIMIT 0,1")->execute($currentPriority->priority_order);
				if ($nextPriority->numRows > 0) //Is there a previous round?
				{
					//Update current round
					$this->Database->prepare("UPDATE tl_tm_priorities SET priority_order=? WHERE id=?")->execute($nextPriority->priority_order,$currentPriority->id);
					//Update previous round
					$this->Database->prepare("UPDATE tl_tm_priorities SET priority_order=? WHERE id=?")->execute($currentPriority->priority_order,$nextPriority->id);
				}
				//Go back to inital page. Otherwise contao will show a blank list
				$this->redirect('contao/main.php?do=tl_tm_priorities&id=' . $currentPriority->pid);
				break;
		}//switch($act)
	}//public function processOnload
}

?>